import { ElMessage } from 'element-plus'
import router from './router'
import { useUserStore } from './stores/user'

// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  const token = localStorage.getItem('TOKEN')
  document.title = `${to.meta.title}`
  const userStore = useUserStore()
  // 无token
  if (!token) {
    if (to.path == '/login') {
      next()
    } else {
      ElMessage({ type: 'error', message: '请登录后再试' })
      userStore.logout()
    }
    // 有token
  } else {
    if (to.path === '/login') {
      next({ path: '/home' })
    } else {
      next()
    }
  }
})
